package com.etc.baolema.controller;

import com.etc.baolema.entity.Pager;
import com.etc.baolema.entity.Store;
import com.etc.baolema.service.StoreService;
import com.etc.baolema.service.impl.StoreServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({"/store.register", "/store.login", "/store.checkAccount", "/store.checkSname", "/store.queryAll", "/store.queryBySname", "/update_store",
        "/query_store_id","/query_store_idByManager","/store.checkSname1","/store.delete"})
public class StoreServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StoreService service = new StoreServiceImpl();
        String url = request.getRequestURI();
        String action = url.substring(url.lastIndexOf("/") + 1);
        Gson gson = new Gson();
        PrintWriter out = response.getWriter();
        Map<String, Object> resultMap = new HashMap<>();
        if ("store.register".equals(action)) {
            String suser = request.getParameter("suser");
            String spwd = request.getParameter("spwd");
            String sname = request.getParameter("sname");
            String saddress = request.getParameter("saddress");
            String phone = request.getParameter("phone");
            String stype = request.getParameter(("stype"));
            Store store = new Store();
            store.setSname(sname);
            store.setSpwd(spwd);
            store.setSuser(suser);
            store.setSaddress(saddress);
            store.setPhone(phone);
            store.setStype(stype);
            int result = service.ADDStore(store);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "注册成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "注册失败");
            }
        } else if ("store.checkAccount".equals(action)) {
            String suser = request.getParameter("suser");
            Store store = service.login(suser);
            if (store == null) {
                resultMap.put("code", 200);
                resultMap.put("msg", "账号可以使用");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "用户名已存在");
            }

        } else if ("store.checkSname".equals(action)) {
            String sname = request.getParameter("sname");
            if (sname != null && sname != "") {
                Store store1 = service.checkSname(sname);
                if (store1 != null) {
                    resultMap.put("code", 201);
                    resultMap.put("msg", "商家名称已存在");
                } else {
                    resultMap.put("code", 200);
                    resultMap.put("msg", "该名称可以使用");
                }
            }
        } else if ("store.login".equals(action)) {
            String suser = request.getParameter("suser");
            String spwd = request.getParameter("spwd");
            Store store = service.login(suser);

            if (store == null) {
                resultMap.put("code", 202);
                resultMap.put("msg", "账号不存在");
            } else if (!store.getSpwd().equals(spwd)) {
                resultMap.put("code", 201);
                resultMap.put("msg", "密码输入有误");
            } else {
                resultMap.put("code", 200);
                resultMap.put("msg", "登录成功");
                resultMap.put("result", store);
                request.getSession().setAttribute("result",store);
            }
        } else if ("store.queryAll".equals(action)) {
            List<Store> list = service.queryAllStore();
            int currPage = 1;
            int pageNum = 2;
            try {
                currPage = Integer.parseInt(request.getParameter("currPage"));
                pageNum = Integer.parseInt(request.getParameter("pageNum"));
            } catch (Exception e) {
            }
            Pager<Store> page = new Pager(currPage,pageNum,list);
            List<Store> result = service.queryAllStoreByPage(page);
            if (result.size() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询数据成功");
                resultMap.put("result", result);
                resultMap.put("page", page);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "暂无类别数据");
            }
        } else if ("store.queryBySname".equals(action)) {
            String sname = request.getParameter("sname");
            List<Store> list = service.queryBysname(sname);
            int currPage = 1;
            int pageNum = 2;
            try {
                currPage = Integer.parseInt(request.getParameter("currPage"));
                pageNum = Integer.parseInt(request.getParameter("pageNum"));
            } catch (Exception e) {
            }
            Pager<Store> page = new Pager(currPage,pageNum,list);
            List<Store> result = service.queryBysname(sname,page);
            if (list.size() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询数据成功");
                resultMap.put("result", result);
                resultMap.put("page", page);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "暂无类别数据");
            }
        }else if ("query_store_id".equals(action)) {
            int sid = ((Store) request.getSession().getAttribute("store")).getSid();
            Store store = service.queryById(sid);
            if (store != null) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询成功");
                resultMap.put("result", store);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "没有该信息");
            }
        }else if ("query_store_idByManager".equals(action)) {
            int sid = Integer.parseInt(request.getParameter("sid"));
            Store store = service.queryById(sid);
            if (store != null) {
                resultMap.put("code", 200);
                resultMap.put("msg", "查询成功");
                resultMap.put("result", store);
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "没有该信息");
            }
        }else if ("update_store".equals(action)) {
            int sid= Integer.parseInt(request.getParameter("sid"));
            String sname = request.getParameter("sname");
            String stype = request.getParameter("stype");
            String saddress = request.getParameter("saddress");
            String phone = request.getParameter("phone");
            Store store = new Store();
            store.setSid(sid);
            store.setSname(sname);
            store.setStype(stype);
            store.setSaddress(saddress);
            store.setPhone(phone);
            int result = service.update(store);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "修改成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "修改失败");
            }
        }else if("store.delete".equals(action)){
            int sid= Integer.parseInt(request.getParameter("sid"));
            int result = service.deleteStore(sid);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "删除成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "删除失败");
            }
        }
        String str = gson.toJson(resultMap);
        out.print(str);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
